﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using V5_Model;
using V5_Common;
using V5_DAL.SqlServer;

namespace V5_BLL {
    public class BLLProductField {
        DALProductField dal = new DALProductField();
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID) {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelProductField model) {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelProductField model) {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID) {

            dal.Delete(ID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelProductField GetModel(int ID) {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public ModelProductField GetModelByCache(int ID) {

            string CacheKey = "ModelProductFieldModel-" + ID;
            object objModel = DataCacheHelper.GetCache(CacheKey);
            if (objModel == null) {
                try {
                    objModel = dal.GetModel(ID);
                    if (objModel != null) {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCacheHelper.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (ModelProductField)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelProductField> GetModelList(string strWhere) {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelProductField> DataTableToList(DataTable dt) {
            List<ModelProductField> modelList = new List<ModelProductField>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0) {
                ModelProductField model;
                for (int n = 0; n < rowsCount; n++) {
                    model = new ModelProductField();
                    if (dt.Rows[n]["ID"].ToString() != "") {
                        model.ID = int.Parse(dt.Rows[n]["ID"].ToString());
                    }
                    if (dt.Rows[n]["FClassID"].ToString() != "") {
                        model.FClassID = int.Parse(dt.Rows[n]["FClassID"].ToString());
                    }
                    model.FieldName = dt.Rows[n]["FieldName"].ToString();
                    model.FieldValues = dt.Rows[n]["FieldValues"].ToString();
                    if (dt.Rows[n]["FieldType"].ToString() != "") {
                        model.FieldType = int.Parse(dt.Rows[n]["FieldType"].ToString());
                    }
                    if (dt.Rows[n]["IsUseImg"].ToString() != "") {
                        model.IsUseImg = int.Parse(dt.Rows[n]["IsUseImg"].ToString());
                    }
                    model.FieldPics = dt.Rows[n]["FieldPics"].ToString();
                    if (dt.Rows[n]["Choice"].ToString() != "") {
                        model.Choice = int.Parse(dt.Rows[n]["Choice"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList() {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return dal.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        #endregion  成员方法

        //获取产品字段属性
        public DataTable GetProductCustomChoice(string strCustomChoice) {
            DataTable table = new DataTable();
            if (strCustomChoice.Trim() != "") {
                ModelProductField modelproductfield = new ModelProductField();
                BLLProductField bllproductfield = new BLLProductField();
                string[] strArray = strCustomChoice.Split(new string[] { "$$$$" }, StringSplitOptions.None);
                int num = 0;
                if (strArray.Length > 0) {
                    table.Columns.Add(new DataColumn("FieldID"));
                    table.Columns.Add(new DataColumn("FieldName"));
                    table.Columns.Add(new DataColumn("FieldValue"));
                    table.Columns.Add(new DataColumn("CustomIndex"));
                    table.Columns.Add(new DataColumn("choice"));//用户选择
                    for (int i = 0; i < strArray.Length; i++)//取得多少项的记录
                    {
                        if ((strArray[i] != null) && (strArray[i] != "")) {
                            int pfID = int.Parse(strArray[i].ToString().Split(new string[] { "||" }, StringSplitOptions.None)[0]);
                            string productFieldName = bllproductfield.GetModel(pfID).FieldName;//字段名称
                            //把字段 的是是否用户可选取出来
                            int choice = InputHelper.SetNumber(bllproductfield.GetModel(pfID).Choice);
                            string str2 = strArray[i].ToString().Split(new string[] { "||" }, StringSplitOptions.None)[1];
                            DataRow row = table.NewRow();
                            row[0] = pfID;
                            row[1] = productFieldName;
                            row[2] = str2;
                            row[3] = num;
                            row[4] = choice;
                            table.Rows.Add(row);
                            num++;
                        }
                    }
                    return table;
                }
            }
            return table;
        }
    }
}
